//
//  RTJRiskAssessmentController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/10.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJRiskAssessmentController.h"
#import "RTJLendCell.h"
#import "RTJRiskResultController.h"

@interface RTJRiskAssessmentController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,assign) int nummber;
@property (nonatomic,strong) NSArray *titleArr;
@property (nonatomic,strong) NSArray *data;
@property (nonatomic,strong) NSMutableDictionary *resultdict;

@end

@implementation RTJRiskAssessmentController

- (void)viewDidLoad {
    [super viewDidLoad];

    _resultdict = [NSMutableDictionary new];
    self.title = @"风险测评";
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.estimatedRowHeight=300;
    
    
    _titleArr = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10"];
    _nummber = 0;

    [self loadData];
}

-(void)loadData{
    
    
    [SVProgressHUD  showWithStatus:nil];
    
    [RTJNetWork asyncRequestWithURL:@"user/risk" params:nil method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.data = data[@"data"];
                [self.tableView reloadData];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

        }
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.data.count!=0 ? 6: 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJLendCell";
    
    RTJLendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (indexPath.row == 0) {
        
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJLendCell" owner:self options:nil][3];
        }
        cell.totalRisk.text = [NSString stringWithFormat:@"/%lu",(unsigned long)_data.count];
        cell.riskNum.text = _titleArr[_nummber];
        cell.riskQuestion.text = _data[_nummber][@"title"];
        
    }else{
        
        NSArray *arr = @[@"A、",@"B、",@"C、",@"D、",@"E、"];
        
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJLendCell" owner:self options:nil][4];
        }
        
        cell.riskAnswer.text = [NSString stringWithFormat:@"%@%@",arr[indexPath.row-1],_data[_nummber][@"answer"][indexPath.row-1][@"content"]];

    }
    
    
//    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row != 0) {
        
        NSString *queStr = [NSString stringWithFormat:@"%@",_data[_nummber][@"id"]];
        NSString *anwStr = [NSString stringWithFormat:@"%@",_data[_nummber][@"answer"][indexPath.row-1][@"id"]];
        
        [_resultdict setObject:anwStr forKey:queStr ];
        
        if (_nummber == _data.count-1) {
            
            RTJRiskResultController *vc = [[RTJRiskResultController alloc]init];
            vc.isReply = _isReply;
            NSData *data = [NSJSONSerialization dataWithJSONObject:_resultdict options:NSJSONWritingPrettyPrinted error:nil];
            NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            vc.jsonStr = jsonStr;
            [self.navigationController pushViewController:vc animated:YES];
            return;
            
        }else{
            
            _nummber++;
            
            [self.tableView reloadData];
        }
    }

}


@end
